package gr.uop.cst.context.adaptation.artifacts.oom;

import gr.uop.cst.context.adaptation.Action;
import java.lang.reflect.Method;

/**
 *
 * @author Ioannis Bourlakos <mi49@uop.gr>
 */
public abstract class AbstractMethodAdaptation extends AbstractMemberAdaptation<Method> {

    private Class[] methodParameters;
    
    protected AbstractMethodAdaptation(Action action, Method m) {
        super(m,action);
    }
    
    public AbstractMethodAdaptation(Action action, String className, String methodName, String ... parameterTypeNames) throws ClassNotFoundException, NoSuchMethodException {
        super(
            Class.forName(className).getDeclaredMethod(
                methodName, 
                parametersFromStringsToClasses(parameterTypeNames)
            )
            , 
            action
        );
        this.methodParameters = parametersFromStringsToClasses(parameterTypeNames);
    }
    
    private static Class[] parametersFromStringsToClasses(String ... params) throws ClassNotFoundException {
        Class[] result = new Class[params.length];
        int i = 0;
        for (String s : params) {
            result[i++] = Class.forName(s);
        }
        return result;
    }

    public Method getMember() throws ClassNotFoundException, NoSuchMethodException {
        return Class.forName(getClassName()).getDeclaredMethod(getMemberName(),methodParameters);
    }
    
    public Method getMethod() throws ClassNotFoundException, NoSuchMethodException {
        return getMember();
    }
    
}
